//http://javaswing.wordpress.com/2009/07/26/jbutton_pressing/

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MyCalculator extends JFrame
{
	private JTextField text = new JTextField();
	private JButton but1 = new JButton("1");
	private JButton but2 = new JButton("2");
	private JButton but3 = new JButton("3");
	private JButton but4 = new JButton("4");
	private JButton but5 = new JButton("5");
	private JButton but6 = new JButton("6");
	private JButton but7 = new JButton("7");
	private JButton but8 = new JButton("8");
	private JButton but9 = new JButton("9");
	private JButton but0 = new JButton("0");
	private JButton butPoint = new JButton(".");
	private JButton butAdd = new JButton("+");
	private JButton butSub = new JButton("-");
	private JButton butMult = new JButton("*");
	private JButton butDiv = new JButton("/");
	private JButton butEquals = new JButton("=");
	private JButton butC = new JButton("C");
	private JFrame mainFrame = new JFrame("MyCalculator");
	
	private boolean pointFlag = true;
	private int numCount = 0;
	private float firstNum = -1;
	private float secondNum = -1;
	private String operation = "";
	
	public void createGUI()
	{	
		mainFrame.setSize(200, 250);
		mainFrame.setResizable(false);
		mainFrame.setLocationRelativeTo(null);
		mainFrame.setLayout(new GridBagLayout());
		text.addKeyListener(new KeyAdapter() 
		{
			@Override
			public void keyTyped(KeyEvent e) 
			{
					e.consume();
			}
		}); 
		
		mainFrame.add(text, new GridBagConstraints(0,0,4,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));

		mainFrame.add(butC, new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(butDiv, new GridBagConstraints(1,1,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(butMult, new GridBagConstraints(2,1,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(butSub, new GridBagConstraints(3,1,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		
		mainFrame.add(but7, new GridBagConstraints(0,2,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(but8, new GridBagConstraints(1,2,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(but9, new GridBagConstraints(2,2,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(butAdd, new GridBagConstraints(3,2,1,2,0,0,GridBagConstraints.NORTH,GridBagConstraints.VERTICAL,
				new Insets(1, 1, 1, 1), 0, 0));
		
		mainFrame.add(but4, new GridBagConstraints(0,3,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(but5, new GridBagConstraints(1,3,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(but6, new GridBagConstraints(2,3,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		
		mainFrame.add(but1, new GridBagConstraints(0,4,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(but2, new GridBagConstraints(1,4,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(but3, new GridBagConstraints(2,4,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(butEquals, new GridBagConstraints(3,4,1,2,0,0,GridBagConstraints.NORTH,GridBagConstraints.VERTICAL,
				new Insets(1, 1, 1, 1), 0, 0));
		
		mainFrame.add(but0, new GridBagConstraints(0,5,2,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		mainFrame.add(butPoint, new GridBagConstraints(2,5,1,1,0,0,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL,
				new Insets(1, 1, 1, 1), 0, 0));
		
		/*Component[] c = mainFrame.getComponents(); 
		for(int i = 0; i < c.length; i++)
		{
			if(c[i] instanceof JButton)
			{
				((JButton)c[i]).addActionListener(new NumButtonActionListener());
			}
		}*/
		but1.addActionListener(new NumButtonActionListener());
		but2.addActionListener(new NumButtonActionListener());
		but3.addActionListener(new NumButtonActionListener());
		but4.addActionListener(new NumButtonActionListener());
		but5.addActionListener(new NumButtonActionListener());
		but6.addActionListener(new NumButtonActionListener());
		but7.addActionListener(new NumButtonActionListener());
		but8.addActionListener(new NumButtonActionListener());
		but9.addActionListener(new NumButtonActionListener());
		but0.addActionListener(new NumButtonActionListener());
		butAdd.addActionListener(new OperationButtonActionListener());
		butSub.addActionListener(new OperationButtonActionListener());
		butDiv.addActionListener(new OperationButtonActionListener());
		butMult.addActionListener(new OperationButtonActionListener());
		butEquals.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				secondNum = Float.parseFloat(text.getText());
				switch(operation)
				{
					case "+": text.setText(Float.toString(firstNum + secondNum)); break;
					case "-": text.setText(Float.toString(firstNum - secondNum)); break;
					case "*": text.setText(Float.toString(firstNum * secondNum)); break;
					case "/": text.setText(Float.toString(firstNum / secondNum)); break;
				}
			}
		});
		butPoint.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent event) {
				if(pointFlag == true)
				{
					if((text.getText()).length() != 0 && numCount < 11)
					{
						pointFlag = false;
						numCount++;
						text.setText(text.getText() + ((JButton)event.getSource()).getText());
					}
				}
			}
		});
		butC.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				numCount = 0;
				pointFlag = true;
				firstNum = -1;
				secondNum = -1;
				text.setText("");
			}
		});
		mainFrame.pack();
		mainFrame.setVisible(true);
	}
	public class NumButtonActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent event) 
		{
			if(text.getText().equals("0") == false && numCount < 11)
			{
				if(numCount == 9 && (new StringBuilder(text.getText()).charAt(8) == '0') &&
						(new StringBuilder(((JButton)event.getSource()).getText())).charAt(0) == '0') return;
				numCount++;
				text.setText(text.getText() + ((JButton)event.getSource()).getText());
			}
		}
	}
	
	public class OperationButtonActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent event) 
		{
			operation = ((JButton)event.getSource()).getText();
			firstNum = Float.parseFloat(text.getText());
			numCount = 0;
			pointFlag = true;
			text.setText("");
		}
		
	}
	public static void main(String[] args)
	{
		MyCalculator m = new MyCalculator();
		m.createGUI();
	}
}